package com.example.jetpack_databinding.main5

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.Observable
import androidx.databinding.ObservableBoolean
import androidx.databinding.ObservableField
import com.example.jetpack_databinding.R
import com.example.jetpack_databinding.databinding.ActivityMain5Binding
import com.example.jetpack_databinding.main4.User
import kotlinx.android.synthetic.main.activity_main5.*

class MainActivity5 : AppCompatActivity() {
    lateinit var model: UserViewmodel
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val activityMain5Binding =
            DataBindingUtil.setContentView<ActivityMain5Binding>(this, R.layout.activity_main5)
        model = UserViewmodel()
        activityMain5Binding.userViewModel = model
        btn.setOnClickListener {
            Log.d("MainActivity5", "" + model.userobservableField.get()?.userName)
            model.userobservableField.set(User("aaaaaa"))
        }
        //监听属性值的变化
        model.userobservableField.addOnPropertyChangedCallback(object :
            Observable.OnPropertyChangedCallback() {
            override fun onPropertyChanged(sender: Observable?, propertyId: Int) {
                Log.d("MainActivity5", "属性值发生变化${model.userobservableField.get()?.userName}")
            }
        })
    }
}

